<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class jadwal_kuliah extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_jadwal_kuliah');
		//$this->output->enable_profiler(TRUE);
	}
	
	function index(){
		$data = array(
			'pilihanTahun' => $this->cek_tahun(true),
		);
		$this->load->view('penjadwalan/jadwal_kuliah', $data);
	}
	
	function grid(){
		$data = $this->mdl_jadwal_kuliah->getdata();
		echo $this->mdl_jadwal_kuliah->togrid($data['row_data'], $data['row_count']);
	}
	
	function add($id_tahunajaran="", $kd_fakultas="", $kd_prodi=""){
		//$data['id_jadwal'] = '';
        //$data['JadwalPar'] = '';
        //$data['JadwalSer'] = '';
        $data['id_tahunajaran'] = $id_tahunajaran.'.'.$kd_prodi;
        //$data['kd_pt'] = '';
        $data['kd_prodi'] = $kd_prodi;
        $data['kd_kelas'] = '';
        $data['kd_jenisjadwal'] = '';
        //$data['AdaResponsi'] = '';
        $data['kd_matakuliah'] = '';
        $data['Nama'] = '';
        $data['id_hari'] = '';
        $data['JamMulai'] = '08:00';
        $data['JamSelesai'] = '10:59';
        $data['TglMulai'] = '';
        $data['TglSelesai'] = '';
        $data['SKS'] = '-1';
        //$data['SKSHonor'] = '';
        $data['kd_dosen'] = '';
        $data['RencanaKehadiran'] = '14';
        //$data['Kehadiran'] = '';
        $data['KehadiranMin'] = '10';
        //$data['JumlahMhsw'] = '';
        //$data['JumlahMhswKRS'] = '';
        //$data['Kapasitas'] = '';
        $data['kd_ruang'] = '';
        //$data['JumlahKelasSerial'] = '';
        //$data['JumlahPraKRS'] = '';
        //$data['Tugas'] = '';
        //$data['Presensi'] = '';
        //$data['UTS'] = '';
        //$data['UAS'] = '';
        //$data['Final'] = '';
        //$data['Responsi'] = '';
        //$data['UTSTanggal'] = '';
        //$data['UTSJamMulai'] = '';
        //$data['UTSJamSelesai'] = '';
        //$data['UTSRuangID'] = '';
        //$data['UASTanggal'] = '';
        //$data['UASJamMulai'] = '';
        //$data['UASJamSelesai'] = '';
        //$data['UASRuangID'] = '';
        
        $data['kode'] = '';
		$data['kd_fakultas'] = $kd_fakultas;
		//$data['kd_kurikulum'] = '';
		
		$this->load->view('penjadwalan/jadwal_kuliah_form', $data);
	}
	
	function edit($kode){
		$r = $this->mdl_jadwal_kuliah->getdataedit($kode);
		
        //$data['id_jadwal'] = '';
        //$data['JadwalPar'] = '';
        //$data['JadwalSer'] = '';
        $data['id_tahunajaran'] = $r->row()->id_tahunajaran.'.'.$r->row()->kd_prodi;
        //$data['kd_pt'] = '';
        $data['kd_prodi'] = $r->row()->kd_prodi;
        $data['kd_kelas'] = $r->row()->kd_kelas;
        $data['kd_jenisjadwal'] = $r->row()->kd_jenisjadwal;
        //$data['AdaResponsi'] = '';
        $data['kd_matakuliah'] = $r->row()->kd_matakuliah;
        //$data['Nama'] = '';
        $data['id_hari'] = $r->row()->id_hari;
        $data['JamMulai'] = substr($r->row()->JamMulai, 0, 5);
        $data['JamSelesai'] = substr($r->row()->JamSelesai, 0, 5);
        $data['TglMulai'] = $r->row()->TglMulai;
        $data['TglSelesai'] = $r->row()->TglSelesai;
        $data['SKS'] = $r->row()->SKS;
        //$data['SKSHonor'] = '';
        $data['kd_dosen'] = $r->row()->kd_dosen;
        $data['RencanaKehadiran'] = $r->row()->RencanaKehadiran;
        //$data['Kehadiran'] = '';
        $data['KehadiranMin'] = $r->row()->KehadiranMin;
        //$data['JumlahMhsw'] = '';
        //$data['JumlahMhswKRS'] = '';
        //$data['Kapasitas'] = '';
        $data['kd_ruang'] = $r->row()->kd_ruang;
        //$data['JumlahKelasSerial'] = '';
        //$data['JumlahPraKRS'] = '';
        //$data['Tugas'] = '';
        //$data['Presensi'] = '';
        //$data['UTS'] = '';
        //$data['UAS'] = '';
        //$data['Final'] = '';
        //$data['Responsi'] = '';
        //$data['UTSTanggal'] = '';
        //$data['UTSJamMulai'] = '';
        //$data['UTSJamSelesai'] = '';
        //$data['UTSRuangID'] = '';
        //$data['UASTanggal'] = '';
        //$data['UASJamMulai'] = '';
        //$data['UASJamSelesai'] = '';
        //$data['UASRuangID'] = '';
		
		$data['kode'] = $kode;
		$data['kd_fakultas'] = $r->row()->kd_fakultas;
		//$data['kd_kurikulum'] = $r->row()->kd_kurikulum;
		
		$this->load->view('penjadwalan/jadwal_kuliah_form', $data);
	}
	
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		$mk = $this->mdl_jadwal_kuliah->getMatakuliah($data['kd_matakuliah']);

		$data['id_tahunajaran'] = explode('.', $data['id_tahunajaran']);
		$data['id_tahunajaran'] = $data['id_tahunajaran'][0];
		
		if($mk->num_rows() > 0){
			if($data['SKS'] == '-1'){
				$data['SKS'] = $mk->row()->sks;
			}
			$data['Nama'] = $mk->row()->nama_matakuliah;
		}else{
			$data['Nama'] = '';
		}
		
		# rules validasi form
        $this->form_validation->set_rules('id_tahunajaran', 'id_tahunajaran', 'trim|required|xss_clean');
        $this->form_validation->set_rules('kd_prodi', 'kd_prodi', 'trim|required|xss_clean');
        $this->form_validation->set_rules('kd_kelas', 'kd_kelas', 'trim|required|xss_clean');
        $this->form_validation->set_rules('kd_jenisjadwal', 'kd_jenisjadwal', 'trim|required|xss_clean');
        $this->form_validation->set_rules('kd_matakuliah', 'kd_matakuliah', 'trim|required|xss_clean');
        $this->form_validation->set_rules('id_hari', 'id_hari', 'trim|required|xss_clean');
        $this->form_validation->set_rules('JamMulai', 'JamMulai', 'trim|required|xss_clean');
        $this->form_validation->set_rules('JamSelesai', 'JamSelesai', 'trim|required|xss_clean');
        $this->form_validation->set_rules('SKS', 'SKS', 'trim|required|xss_clean');
        $this->form_validation->set_rules('kd_dosen', 'kd_dosen', 'trim|required|xss_clean');
        $this->form_validation->set_rules('RencanaKehadiran', 'RencanaKehadiran', 'trim|required|xss_clean');
        $this->form_validation->set_rules('KehadiranMin', 'KehadiranMin', 'trim|required|xss_clean');
        $this->form_validation->set_rules('kd_ruang', 'kd_ruang', 'trim|required|xss_clean');
        
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_biaya',' ',' '))==''?'':form_error('kd_biaya',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			if($aksi=="add"){ // add
				//$result = !$this->mdl_jadwal_kuliah->isExistKode($data);
				//if ($result){
					$result = $this->mdl_jadwal_kuliah->InsertOnDb($data);
				// }else{
					// $data['pesan_error'] .= 'Sudah terdapat nim dalam database!';
				// }
			}else { // edit
				//if($data['kode'] == $data['id_tahunajaran'].$data['semester']){ // jika kode tidak diubah
					$result = $this->mdl_jadwal_kuliah->UpdateOnDb($data);
				// }else{
					// $result = !$this->mdl_jadwal_kuliah->isExistKode($data);
					// if ($result){
						// $result=$this->mdl_jadwal_kuliah->UpdateOnDb($data);
					// }else{
						// $data['pesan_error'] .= 'Sudah terdapat kode dalam database!';
					// }
				// }
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
		
	} 
	
	// fungsi delet dengan mengganti status A menjadi D
	function delete($id){
		$result = $this->mdl_jadwal_kuliah->DeleteOnDb($id);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}
		
		# cek krs
	function cek_tahun($return = false){
		$thn = $this->mdl_jadwal_kuliah->get_tahuns();
		if($thn->num_rows() > 0) {
			$thnV = $thn->result();
			foreach($thnV as $tahun) {
				if($this->mdl_jadwal_kuliah->cek_tahun($tahun->id_tahunajaran)) {
					if($return) {
						return $tahun->id_tahunajaran;
					}
					echo $tahun->id_tahunajaran;
					return;
				}
			}
		}
		if($return)
			return false;
		echo 'false';
	}

}